package com.fhz.exam001.fragment;

import android.os.AsyncTask;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.fhz.exam001.R;
import com.fhz.exam001.adapter.FoodAdapter;
import com.fhz.exam001.entity.Food;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;


public class IndexFragment extends Fragment {
    private ListView lv;

    private FoodAdapter foodAdapter;
    private List<Food.Data> foodList;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_index, container, false);
        lv = (ListView) inflate.findViewById(R.id.lv);

        new MyTask().execute("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");
        foodList = new ArrayList<>();
        foodAdapter = new FoodAdapter(getActivity(), foodList);
        lv.setAdapter(foodAdapter);

        return inflate;
    }

    public class MyTask extends AsyncTask<String,Void,String>{
        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//                    urlConnection.setConnectTimeout();
//                    urlConnection.setRequestMethod("GET");
                if (httpURLConnection.getResponseCode()  == 200) {
                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    String str = "";
                    StringBuilder stringBuilder = new StringBuilder();
                    while ((str = bufferedReader.readLine()) != null) {
                        stringBuilder.append(str);
                    }
                    String s = stringBuilder.toString();
                    return s;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if(s != null){
                Food food = new Gson().fromJson(s, Food.class);
                List<Food.Data> data = food.getData();
                foodList.clear();
                foodList.addAll(data);
                foodAdapter.notifyDataSetChanged();
            }
        }

    }
}